<?php
define('IN_SITE',true);
define('PLUG_NAME','hello_world');

if(!defined('APP_ROOT')){
	define('PLUG_ROOT',APP_ROOT);
}else{
	define('PLUG_ROOT',APP_ROOT.'/plugin/'.PLUG_NAME);
}

function db_init(){
	$root = str_replace('\\','/',dirname(__FILE__));
	$root = str_replace('/Plugin/hello_world','',$root);

	$db_config = include $root.'/Conf/dbconfig.php';
	$db_config = $db_config['dbw'];
	$db_host = $db_config['db_host'];
	$db_user = $db_config['db_user'];
	$db_pswd = $db_config['db_pswd'];
	$db_name = $db_config['db_name'];
	$db_charset = $db_config['db_charset'];
	$db_link = mysql_connect($db_host,$db_user,$db_pswd) or die('Not connected : ' . mysql_error());
	$select_db = mysql_select_db($db_name,$db_link) or die("Can't use foo : " . mysql_error());
	mysql_set_charset($db_charset,$db_link); 

	return $db_link;
}

function run_sql($sql){
	$db_link = db_init();
	$result = mysql_query($sql,$db_link) or die(mysql_error());
    mysql_close($db_link);

	return $result;
}

function get_data($sql){
	$db_link = db_init();
	$result = mysql_query($sql,$db_link) or die(mysql_error());
	$data = array();
	while ($row = mysql_fetch_assoc($result)) {
        $data[] = $row;
    }
	mysql_free_result($result);
    mysql_close($db_link);

	return $data;
}

function get_vote($id){
	$sql = "SELECT * FROM `plugin_vote_main` WHERE `main_id`='{$id}' AND `main_status`='1' LIMIT 1";
	$vote = get_data($sql);

	return $vote[0];
}

function show_vote($id){
	$vote = get_vote($id);
	$html = '<h3 class="vote">'.$vote['main_title'].'</h3>'
		   .'<p>'.$vote['main_intro'].'</p>';
	
	echo $html;
}